#include "CategoryList.h"
#include "Priority.h"
#include "Profile.h"


CategoryList:: CategoryList(){}

void CategoryList :: fileToVector ()
{
	Category newCategory, emptyCategory;

	string line;

	ifstream accomodationFile ("Accomodation.txt");
	if (accomodationFile.is_open())
	{
		while ( accomodationFile.good() )
		{
			getline (accomodationFile,line);
			newCategory.addLog(line);
		}
		accomodationFile.close();
		allCategory.push_back(newCategory);
	}
	else cout << "Unable to open file";
	newCategory = emptyCategory;

	ifstream academicsFile ("Academics.txt");
	if (academicsFile.is_open())
	{
		while ( academicsFile.good() )
		{
			getline ( academicsFile,line);
			newCategory.addLog(line);
		}
		academicsFile.close();
		allCategory.push_back(newCategory);
	}
	else cout << "Unable to open file";
	newCategory = emptyCategory;

	ifstream debtFile ("Debts.txt");
	if (debtFile.is_open())
	{
		while ( debtFile.good() )
		{
			getline (debtFile,line);
			newCategory.addLog(line);
		}
		debtFile.close();
		allCategory.push_back(newCategory);
	}
	else cout << "Unable to open file";
	newCategory = emptyCategory;	

	ifstream foodFile ("Food.txt");
	if (foodFile.is_open())
	{
		while ( foodFile.good() )
		{
			getline (foodFile,line);
			newCategory.addLog(line);
		}
		foodFile.close();
		allCategory.push_back(newCategory);
	}
	else cout << "Unable to open file";
	newCategory = emptyCategory;

	ifstream recreationFile ("Recreation.txt");
	if (recreationFile.is_open())
	{
		while ( recreationFile.good() )
		{
			getline (recreationFile,line);
			newCategory.addLog(line);
		}
		recreationFile.close();
		allCategory.push_back(newCategory);
	}
	else cout << "Unable to open file";
	newCategory = emptyCategory;

	ifstream transportationFile ("Transportation.txt");
	if (transportationFile.is_open())
	{
		while ( transportationFile.good() )
		{
			getline (transportationFile,line);
			newCategory.addLog(line);
		}
		transportationFile.close();
		allCategory.push_back(newCategory);
	}
	else cout << "Unable to open file";
	newCategory = emptyCategory;

	ifstream othersFile ("Others.txt");
	if (othersFile.is_open())
	{
		while ( othersFile.good() )
		{
			getline (othersFile,line);
			newCategory.addLog(line);
		}
		othersFile.close();
		allCategory.push_back(newCategory);
	}
	else cout << "Unable to open file";
	newCategory = emptyCategory;

	ifstream ProfileFile ("Profile.txt");
	if (ProfileFile.is_open())
	{
		while ( ProfileFile.good() )
		{
			getline (ProfileFile,line);
			stringstream ss;
			ss<<line;
			string temp;
			ss>>temp;
			profile.setName(temp);
			ss>>temp;
			profile.setCountry(temp);

		}
		ProfileFile.close();
	
	}
	else cout << "Unable to open file";
	
	ifstream PriorityFile ("Priority.txt");
	if (PriorityFile.is_open())
	{
		while ( PriorityFile.good() )
		{
			getline (PriorityFile,line);
			stringstream ss;
			ss<<line;
			int temp;
			ss>>temp;
			priority.setAccomodation(temp);
			ss>>temp;
			priority.setAcademics(temp);
			ss>>temp;
			priority.setDebts(temp);
			ss>>temp;
			priority.setFood(temp);
			ss>>temp;
			priority.setRecreation(temp);
			ss>>temp;
			priority.setTransportation(temp);
			ss>>temp;
			priority.setOthers(temp);

		}
		PriorityFile.close();
	
	}
	else cout << "Unable to open file";

}

void CategoryList ::vectorToFile()
{
	ofstream accomodationFile ("Accomodation.txt");
	if (accomodationFile.is_open())
	{
		if(allCategory[0].logList.empty() == 1)
			accomodationFile.close();
		else
		{
			for(size_t i =0; i < allCategory[0].logList.size(); i++)
			{
				accomodationFile << allCategory[0].logList[i].getDate().getDay();
				accomodationFile << " ";
				accomodationFile << allCategory[0].logList[i].getDate().getMonth();
				accomodationFile << " ";
				accomodationFile << allCategory[0].logList[i].getDate().getYear();
				accomodationFile << " ";
				accomodationFile << allCategory[0].logList[i].getAmount();
				accomodationFile << " ";
				accomodationFile << allCategory[0].logList[i].getComment();
				if( i+1 == allCategory[0].logList.size())
					accomodationFile << " ";
				else
					accomodationFile << "\n";
			}
		}
		accomodationFile.close();
	}
	else cout << "Unable to open file";


	ofstream academicsFile ("Academics.txt");
	if (academicsFile.is_open())
	{
		for(size_t i =0; i < allCategory[1].logList.size(); i++)
		{
			academicsFile << allCategory[1].logList[i].getDate().getDay();
			academicsFile << " ";
			academicsFile << allCategory[1].logList[i].getDate().getMonth();
			academicsFile << " ";
			academicsFile << allCategory[1].logList[i].getDate().getYear();
			academicsFile << " ";
			academicsFile << allCategory[1].logList[i].getAmount();
			academicsFile << " ";
			academicsFile << allCategory[1].logList[i].getComment();
			academicsFile << " ";
			if( i+1 == allCategory[1].logList.size())
				academicsFile << " ";
			else
				academicsFile << "\n";
		}
		academicsFile.close();

	}
	else cout << "Unable to open file";


	ofstream debtFile ("Debts.txt");
	if (debtFile.is_open())
	{
		for(size_t i =0; i < allCategory[2].logList.size(); i++)
		{
			debtFile << allCategory[2].logList[i].getDate().getDay();
			debtFile << " ";
			debtFile << allCategory[2].logList[i].getDate().getMonth();
			debtFile << " ";
			debtFile << allCategory[2].logList[i].getDate().getYear();
			debtFile << " ";
			debtFile << allCategory[2].logList[i].getAmount();
			debtFile << " ";
			debtFile << allCategory[2].logList[i].getComment();
			debtFile << " ";
			if( i+1 == allCategory[2].logList.size())
				debtFile << " ";
			else
				debtFile << "\n";
		}
		debtFile.close();
	}
	else cout << "Unable to open file";


	ofstream foodFile ("Food.txt");
	if (foodFile.is_open())
	{
		for(size_t i =0; i < allCategory[3].logList.size(); i++)
		{
			foodFile << allCategory[3].logList[i].getDate().getDay();
			foodFile << " ";
			foodFile << allCategory[3].logList[i].getDate().getMonth();
			foodFile << " ";
			foodFile << allCategory[3].logList[i].getDate().getYear();
			foodFile << " ";
			foodFile << allCategory[3].logList[i].getAmount();
			foodFile << " ";
			foodFile << allCategory[3].logList[i].getComment();
			foodFile << " ";
			if( i+1 == allCategory[3].logList.size())
				foodFile << " ";
			else
				foodFile << "\n";
		}
		foodFile.close();
	}
	else cout << "Unable to open file";


	ofstream recreationFile ("Recreation.txt");
	if (recreationFile.is_open())
	{
		for(size_t i =0; i < allCategory[4].logList.size(); i++)
		{
			recreationFile << allCategory[4].logList[i].getDate().getDay();
			recreationFile << " ";
			recreationFile << allCategory[4].logList[i].getDate().getMonth();
			recreationFile << " ";
			recreationFile << allCategory[4].logList[i].getDate().getYear();
			recreationFile << " ";
			recreationFile << allCategory[4].logList[i].getAmount();
			recreationFile << " ";
			recreationFile << allCategory[4].logList[i].getComment();
			recreationFile << " ";
			if( i+1 == allCategory[4].logList.size())
				recreationFile << " ";
			else
				recreationFile << "\n";
		}
		recreationFile.close();
	}
	else cout << "Unable to open file";

	ofstream transportationFile ("Transportation.txt");
	if (transportationFile.is_open())
	{	
		for(size_t i =0; i < allCategory[5].logList.size(); i++)
		{
			transportationFile << allCategory[5].logList[i].getDate().getDay();
			transportationFile << " ";
			transportationFile << allCategory[5].logList[i].getDate().getMonth();
			transportationFile << " ";
			transportationFile << allCategory[5].logList[i].getDate().getYear();
			transportationFile << " ";
			transportationFile << allCategory[5].logList[i].getAmount();
			transportationFile << " ";
			transportationFile << allCategory[5].logList[i].getComment();
			transportationFile << " ";
			if( i+1 == allCategory[5].logList.size())
				transportationFile << " ";
			else
				transportationFile << "\n";
		}
		transportationFile.close();
	}
	else cout << "Unable to open file";


	ofstream othersFile ("Others.txt");
	if (othersFile.is_open())
	{
		for(size_t i =0; i < allCategory[6].logList.size(); i++)
		{
			othersFile << allCategory[6].logList[i].getDate().getDay();
			othersFile << " ";
			othersFile << allCategory[6].logList[i].getDate().getMonth();
			othersFile << " ";
			othersFile << allCategory[6].logList[i].getDate().getYear();
			othersFile << " ";
			othersFile << allCategory[6].logList[i].getAmount();
			othersFile << " ";
			othersFile << allCategory[6].logList[i].getComment();
			othersFile << " ";
			if( i+1 == allCategory[6].logList.size())
				othersFile << " ";
			else
				othersFile << "\n";
		}
		othersFile.close();
	}
	else cout << "Unable to open file";

	/**

	ofstream PriorityFile ("Priority.txt");
	if (PriorityFile.is_open())
	{
			PriorityFile << priority.getAccomodation();
			PriorityFile << " ";
			PriorityFile << priority.getAcademics();
			PriorityFile << " ";
			PriorityFile << priority.getDebts();
			PriorityFile << " ";
			PriorityFile << priority.getFood();
			PriorityFile << " ";
			PriorityFile << priority.getRecration();
			PriorityFile << " ";
			PriorityFile << priority.getTransportation();
			PriorityFile << " ";
			PriorityFile << priority.getOthers();
			PriorityFile.close();
	}
	else cout << "Unable to open file";
	*/
}

void CategoryList :: defaultScr()
{
	int choice = 99;
	system("CLS");
	while(choice !=0)
	{
		cout << "Which category would you like to access? \n1)Accomodation \n2)Academics  \n3)Debts  \n4)Food \n5)Recreation \n6)Transportation \n7)Others \n0)Quit" << endl;
		cin >> choice;
		if (choice == 0)
			break;
		else if ( choice > 0 && choice < 8)
		{
			system("CLS");
			allCategory[choice - 1].defaultScreen();

		}
		else
		{
			system("CLS");
			cout << "Invalid option!" << endl;
		}
	}



}

void CategoryList :: newFile()
{
	int i,n=10;
	string FileName[10] = {"Accomodation", "Academics","Debt", "Food", "Transportation", "Recreation","Others", "Profile", "Priority" };

	for(i=0;i<n;i++)
	{
		ofstream myFile( FileName[i] + ".txt" ) ;
	}

	cout<<"files created"<<endl;

}

